//
//  LBImagePicker.swift
//  AFNetworking
//
//  Created by suhayl.wu on 23/12/2019.
//

import Foundation
import LBUIKit
import Photos

@objc public protocol LBImagePickerViewControllerDelegate: class {

    /// Called when the user has selected assets (and tapped the done button).
    ///
    /// - Parameters:
    ///   - pickerViewController: The PickerViewController that was used.
    ///   - assets: The assets the user has selected, in the order the user has selected them.
    func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset])
    
    /// Called when the user tapps the cancel button on the PickerViewController. By default this will dismiss the picker, but a custom implementation has to dismiss the picker.
    ///
    /// - Parameter pickerViewController: The PickerViewController that was used..=
    func pickerViewControllerDidCancel(_ pickerViewController: TatsiPickerViewController)
}

public class LBImagePicker: NSObject {
    
    @objc public weak var delegate: LBImagePickerViewControllerDelegate?
    
    @objc public func show(maxNumberOfSelections: Int, supportedMediaType: String) {
        var config = TatsiConfig.default
        config.maxNumberOfSelections = maxNumberOfSelections
        config.showCameraOption = true
        config.supportedMediaTypes = [PHAssetMediaType.image]
        
        let picker = TatsiPickerViewController(config: config)
        picker.modalPresentationStyle = .fullScreen
        picker.pickerDelegate = self
            
        UIApplication.displayViewController()?.present(picker, animated: true, completion: {
            
        })
    }
}

extension LBImagePicker: TatsiPickerViewControllerDelegate {
    public func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]) {
        self.delegate?.pickerViewController(pickerViewController, didPickAssets: assets)
        pickerViewController.dismiss(animated: true) {
        }
    }

    public func pickerViewControllerDidCancel(_ pickerViewController: TatsiPickerViewController) {
        self.delegate?.pickerViewControllerDidCancel(pickerViewController)
        pickerViewController.dismiss(animated: true) {
        }
    }
    
}
